﻿<%@ Import Namespace="System.IO" %>
<%@ Import Namespace="System" %>
<%@ Import Namespace="System.Drawing" %>
<%@ Import Namespace="System.Drawing.Drawing2D" %>

<%@ Page Language="C#" %>

<%
   
    DateTime dtStart = TimeZone.CurrentTimeZone.ToLocalTime(new DateTime(1970, 1, 1));
    DateTime dtNow = DateTime.Parse(DateTime.Now.ToString());
    TimeSpan toNow = dtNow.Subtract(dtStart);
    string timeStamp = toNow.Ticks.ToString();
    timeStamp = timeStamp.Substring(0, timeStamp.Length - 7);


    String savePath = "/UpLoad/Djack";
    String savePicName = timeStamp;

    string type = Request.Form["type"];
    string typeview = Request.Form["typeview"];
    switch (type)
    {
        case "food":
            savePath = "/UpLoad/niceFood/";
            break;
        case "scenic":
            savePath = "/UpLoad/scenic/";
            break;
        case "specialty":
            savePath = "/UpLoad/specialty/";
            break;
        case "transaction":
            savePath = "/UpLoad/transaction/";
            break;
        case "comments":
            savePath = "/UpLoad/comments/";
            break;
        default:
            savePath = "/UpLoad/Djack/";
            break;
    }

    String file_src = savePath + savePicName + "_src.jpg";

    string filedata = Request.Form["filedata"].ToString();
    filedata = filedata.Substring(filedata.IndexOf("base64,") + 7);

    byte[] base64 = Convert.FromBase64String(filedata);
    //原图
    if (base64.Length > 0)
    {
        try
        {
            if (typeview == "all")
            {
                FileStream fs = new FileStream(Server.MapPath(file_src), System.IO.FileMode.Create);
                fs.Write(base64, 0, base64.Length);
                fs.Close();
            }
            else if (typeview == "part")
            {
                int x = (int)Convert.ToDouble(Request.Form["abscissa"]);
                int y = (int)Convert.ToDouble(Request.Form["ordinate"]);
                int x2 = (int)Convert.ToDouble(Request.Form["abscissa2"]);
                int y2 = (int)Convert.ToDouble(Request.Form["ordinate2"]);
                int width = (int)Convert.ToDouble(Request.Form["width"]);
                int height = (int)Convert.ToDouble(Request.Form["height"]);
                MemoryStream memStream = new MemoryStream(base64);
                //截图保存
                using (var OriginalImage = new Bitmap(memStream))
                {
                    using (var bmp = new Bitmap(width, height, OriginalImage.PixelFormat))
                    {
                        bmp.SetResolution(OriginalImage.HorizontalResolution, OriginalImage.VerticalResolution);
                        using (Graphics Graphic = Graphics.FromImage(bmp))
                        {
                            Graphic.SmoothingMode = SmoothingMode.AntiAlias;
                            Graphic.InterpolationMode = InterpolationMode.HighQualityBicubic;
                            Graphic.PixelOffsetMode = PixelOffsetMode.HighQuality;
                            Graphic.DrawImage(OriginalImage, new Rectangle(0,0, width, height), x, y, width, height, GraphicsUnit.Pixel);
                            var ms = new MemoryStream();
                            bmp.Save(ms, OriginalImage.RawFormat);
                            base64 = ms.GetBuffer();
                            FileStream fs = new FileStream(Server.MapPath(file_src), System.IO.FileMode.Create);
                            fs.Write(base64, 0, base64.Length);
                            fs.Close();

                        }
                    }
                }
            }
            else
            {
                Response.Write("{status:'-1',msg:'没有该类型的图片保存'}");
                return;
            }
        }
        catch (Exception e)
        {
            Response.Write("{status:'-1',msg:'" + e.Message + "'}");
            return;
        }
    }

    String picUrl = savePath + savePicName;

    Response.Write("{status:'1',picUrl:'" + file_src + "'}");

%> 

